package cc.mycraft.ds;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.EntityBlockFormEvent;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.ExplosionPrimeEvent;
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.hanging.HangingBreakEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import com.destroystokyo.paper.event.entity.CreeperIgniteEvent;




public class EntityLimit implements Listener{
	
	@EventHandler
	public void onEntitySpawn(CreatureSpawnEvent e) {
		if(!Spawner.isEnabled()) {
			return;
		}
		if(!e.getLocation().getWorld().getName().equals(Spawner.DungeonWorrldName)) {
			return;
		}
		switch (e.getSpawnReason()) {
		case BUILD_IRONGOLEM: 
			e.getEntity().setMaxHealth(6);
			e.getEntity().addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 1000, 1));
			break;
		case BUILD_SNOWMAN:
			e.getEntity().addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 1000, 1));
			break;
		case REINFORCEMENTS:
			if(e.getEntity().getNearbyEntities(12, 12, 12).size()>5) {
				e.setCancelled(true);
			}
		default:
			break;
		}
	}
	
	@EventHandler
	public void onPlayerTeleport(PlayerTeleportEvent e) {
		if(e.isCancelled()) {
			return;
		}
		if(e.getTo().getWorld().getName().equals(Spawner.DungeonWorrldName)) {
			if(!Spawner.isEnabled()) {
				Spawner.EnableSpawner();
			}
		}
	}
	
	@EventHandler
	public void onPlayerJoin(PlayerJoinEvent e) {
		Player p = e.getPlayer();
		if(p.getWorld().getName().equals(Spawner.DungeonWorrldName)) {
			if(!Spawner.isEnabled()) {
				Spawner.EnableSpawner();
			}
		}
	}
	
	@EventHandler
	public void onPlayerPlaceBlock(BlockPlaceEvent e) {
		switch (e.getBlock().getType()) {
		case TORCH: case REDSTONE_TORCH: case GLOWSTONE: case SEA_LANTERN: case LANTERN: case END_ROD: case CAMPFIRE: case REDSTONE_LAMP:
		case JACK_O_LANTERN: case REDSTONE_WALL_TORCH: case WALL_TORCH: 
			break;
		default:
			return;
		}
		if(!e.getBlock().getWorld().getName().equals("dungeon")) {
			return;
		}
		delayRemoveBlock(e.getBlock(),30);
	}
	
	@EventHandler
	public void onPlayerDigBlock(BlockBreakEvent e) {
		if(e.isCancelled()) {
			return;
		}
		if(!e.getPlayer().getGameMode().equals(GameMode.SURVIVAL)) {
			return;
		}
		if(e.getPlayer().getWorld().getName().equals(Spawner.DungeonWorrldName)) {
			ItemStack i = e.getPlayer().getInventory().getItemInMainHand();
			if(isDigTool(i)) {
				i.setDurability((short) (i.getDurability()+10));
				e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 1200, 2));
			}
		}
	}
	
	public static void delayRemoveBlock(Block b,long second) {
		Bukkit.getScheduler().scheduleSyncDelayedTask(main.getInstance(), 
		new Runnable() {
			@Override
			public void run() {
				b.setType(Material.AIR);
			}
		}, second*20);
	}
	public static boolean isDigTool(ItemStack i) {
		if(i==null) {
			return false;
		}
		switch (i.getType()) {
		case DIAMOND_SWORD:	return true;
		case DIAMOND_SHOVEL: return true;
		case DIAMOND_PICKAXE: return true;
		case DIAMOND_AXE: return true;
		
		case GOLDEN_SWORD: return true;
		case GOLDEN_SHOVEL: return true;
		case GOLDEN_PICKAXE: return true;
		case GOLDEN_AXE: return true;
		
		case IRON_SWORD: return true;
		case IRON_SHOVEL: return true;
		case IRON_PICKAXE: return true;
		case IRON_AXE: return true;
		
		case STONE_SWORD: return true;
		case STONE_SHOVEL: return true;
		case STONE_PICKAXE: return true;
		case STONE_AXE: return true;
		
		case WOODEN_SWORD: return true;
		case WOODEN_SHOVEL: return true;
		case WOODEN_PICKAXE: return true;
		case WOODEN_AXE: return true;
		
		default:
			return false;
		}
	}
	
}
